package com.sh.data.engine.domain.normalization.standard.model.domain;

import com.sh.data.engine.repository.dao.normalization.standard.entity.DictionaryEntity;
import com.sh.data.engine.repository.dao.normalization.standard.entity.DictionaryMappingDetailEntity;
import com.sh.data.engine.repository.dao.normalization.standard.entity.DictionaryMappingReportEntity;
import lombok.Builder;
import lombok.Data;

import java.util.List;

import static java.lang.Math.toIntExact;

/**
 * @Version 1.0 @Author:xinyan @Date:2022/7/20 @Content:
 */
@Data
public class DictionaryStatisticsDomain {

    private DictionaryStatisticsDomain.DictionaryDomain dicTionaryInfo;

    private DictionaryStatisticsDomain.FormCheckDomain formCheckInfo;

    private DictionaryStatisticsDomain.FieldCheckDomain fieldCheckInfo;

    @Data
    @Builder(toBuilder = true)
    public static class DictionaryDomain {

        private Integer dictionarieTotal;

        private Integer publish;

        private Integer disPublish;
    }

    /**
     * 计算字典统计数据
     *
     * @param list 字典list
     * @return 统计
     */
    public static DictionaryDomain getDicStatistics(
        List<DictionaryEntity> list) {

        return DictionaryDomain.builder()
            .dictionarieTotal(toIntExact(list.size()))
            .publish(toIntExact(list.stream().filter(DictionaryEntity::isRelased).count()))
            .disPublish(toIntExact(list.stream().filter(DictionaryEntity::disRelased).count()))
            .build();
    }

    @Data
    @Builder(toBuilder = true)
    public static class FormCheckDomain {

        private Integer checkFormTotal;

        private Integer standard;

        private Integer disStandard;
    }

    /**
     * 检查表单总数
     *
     * @param list 表单list
     * @return 统计
     */
    public static FormCheckDomain getFormStatistics(
        List<DictionaryMappingReportEntity> list) {
        return FormCheckDomain.builder()
            .checkFormTotal(toIntExact(list.size()))
            .standard(
                toIntExact(list.stream().filter(DictionaryMappingReportEntity::checkIsHitCount).count()))
            .disStandard(
                toIntExact(list.stream().filter(DictionaryMappingReportEntity::checkDisHitCount).count()))
            .build();
    }

    @Data
    @Builder(toBuilder = true)
    public static class FieldCheckDomain {

        private Integer checkFieldTotal;

        private Integer standard;

        private Integer disStandard;
    }

    /**
     * 检查字段总数
     *
     * @param list 字段list
     * @return 统计
     */
    public static FieldCheckDomain getFieldStatistics(
        List<DictionaryMappingDetailEntity> list) {
        return FieldCheckDomain.builder()
            .checkFieldTotal(toIntExact(list.size()))
            .standard(
                toIntExact(list.stream().filter(d -> d.getBoundStatus() != null && 0 < d.getBoundStatus()).count()))
            .disStandard(
                toIntExact(list.stream().filter(d -> d.getBoundStatus() != null && 0 == d.getBoundStatus()).count()))
            .build();
    }

    /**
     * 计数据
     *
     * @return 统计
     */
    public static DictionaryStatisticsDomain newStatDomain(
        List<DictionaryEntity> dic,
        List<DictionaryMappingReportEntity> reportList,
        List<DictionaryMappingDetailEntity> fieldList) {
        DictionaryStatisticsDomain domain = new DictionaryStatisticsDomain();
        domain.setDicTionaryInfo(DictionaryStatisticsDomain.getDicStatistics(dic));

        domain.setDicTionaryInfo(DictionaryStatisticsDomain.getDicStatistics(dic));
        domain.setFormCheckInfo(DictionaryStatisticsDomain.getFormStatistics(reportList));
        domain.setFieldCheckInfo(DictionaryStatisticsDomain.getFieldStatistics(fieldList));
        return domain;
    }
}
